
package ly.gupao.edu;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.SSLContext;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import ly.gupao.edu.wecredo.ZipUtil;
import org.apache.commons.io.IOUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * This example demonstrates how to create secure connections with a custom SSL
 * context.
 */
public class ClientCustomSSL {

    public final static void main(String[] args) throws Exception {
        KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
        FileInputStream instream = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\wecredo-deploy\\wecredo-deploy\\server.keystore"));
        try {
            trustStore.load(instream, "wecredo".toCharArray());
        } finally {
            instream.close();
        }

        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom()
                .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
                .build();
        // Allow TLSv1 protocol only
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,
                new String[] { "TLSv1" },
                null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        CloseableHttpClient httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .build();
        try {

//            HttpGet httpget = new HttpGet("https://localhost/");
            HttpPost httpPost = new HttpPost("https://47.92.246.109/base_data/gs/info.do");
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
            formparams.add(new BasicNameValuePair("param", "cPNu2m%2B%2Fgp9MSnkQ%2FCsqETWl8QYl60dpnAlZc7OV0hKtGXydmA162qXTUzLIGshHt9TVM5aTbO4n%0D%0AKcQMEt5gK4X4%2FybuI%2FvQRUcBqpobkk2LLQ3YTMaBlEB9ckhfPJ40bVQp%2BRMmsLHydFZgW5ciSYV%2F%0D%0Azf8p08f4J9Tfzjl9y%2Bm6MfKVUYBpcq4whTsK2A0L"));
            formparams.add(new BasicNameValuePair("sign", "7e42701511c58eaa5815333fb25519f8"));
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
            httpPost.setEntity(urlEncodedFormEntity);


            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {
                InputStream in = null;
                String result = null;
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    in = entity.getContent();
                    result = IOUtils.toString(in, "utf-8");
                }
                JSONObject jsonObject = JSONObject.parseObject(result);

                result = ZipUtil.getContent(result);
                System.out.println(result);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

}